package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.domain.RegGroup;
import com.ruoyi.system.domain.RunRecords;
import com.ruoyi.system.service.IAppmanagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Sub;
import com.ruoyi.system.service.ISubService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目属性回显定义Controller
 * 
 * @author yaozh
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/system/sub")
public class SubController extends BaseController
{
    @Resource
    private ISubService subService;
    @Resource
    private IAppmanagerService appManagerService;

    /**
     * 查询项目属性回显定义列表
     */
    @PreAuthorize("@ss.hasPermi('system:sub:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sub sub,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Sub> list = subService.selectSubList(sub, dbName);
        return getDataTable(list);
    }

    /**
     * 导出项目属性回显定义列表
     */
    @PreAuthorize("@ss.hasPermi('system:sub:export')")
    @Log(title = "项目属性回显定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sub sub,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<Sub> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = subService.selectSubList(sub, dbName);
        }
        ExcelUtil<Sub> util = new ExcelUtil<Sub>(Sub.class);
        util.exportExcel(response, list, "项目属性回显定义数据");
    }

    @Log(title = "项目属性回显定义", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:sub:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<Sub> util = new ExcelUtil<Sub>(Sub.class);
        List<Sub> subList = util.importExcel(file.getInputStream());
        String operaName = getUsername();
        String message = subService.importSub(subList, updateSupport, operaName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Sub> util = new ExcelUtil<Sub>(Sub.class);
        util.importTemplateExcel(response, "项目属性回显定义数据");
    }

    /**
     * 获取项目属性回显定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sub:query')")
    @GetMapping(value = "/{subid}")
    public AjaxResult getInfo(@PathVariable("subid") Long subid,@RequestParam String dbName)
    {
        return success(subService.selectSubBySubid(subid, dbName));
    }

    /**
     * 新增项目属性回显定义
     */
    @PreAuthorize("@ss.hasPermi('system:sub:add')")
    @Log(title = "项目属性回显定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sub sub,@RequestParam String dbName)
    {
        return toAjax(subService.insertSub(sub, dbName));
    }

    /**
     * 修改项目属性回显定义
     */
    @PreAuthorize("@ss.hasPermi('system:sub:edit')")
    @Log(title = "项目属性回显定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sub sub,@RequestParam String dbName)
    {
        return toAjax(subService.updateSub(sub, dbName));
    }

    /**
     * 删除项目属性回显定义
     */
    @PreAuthorize("@ss.hasPermi('system:sub:remove')")
    @Log(title = "项目属性回显定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subids}")
    public AjaxResult remove(@PathVariable Long[] subids,@RequestParam String dbName)
    {
        return toAjax(subService.deleteSubBySubids(subids, dbName));
    }
}
